home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9310.ZIP / DFPP03.ZIP / KEYBOARD.CPP < prev    next >
C/C++ Source or Header  |  1993-08-15  |  2KB  |  100 lines

  1. // ----------- keyboard.cpp
  2.  
  3. #include <stdio.h>
  4. #include <bios.h>
  5. #include <dos.h>
  6. #include <conio.h>
  7. #include "desktop.h"
  8.  
  9. /* ----- table of alt keys for finding shortcut keys ----- */
  10. static int altconvert[] = {
  11.     ALT_A,ALT_B,ALT_C,ALT_D,ALT_E,ALT_F,ALT_G,ALT_H,
  12.     ALT_I,ALT_J,ALT_K,ALT_L,ALT_M,ALT_N,ALT_O,ALT_P,
  13.     ALT_Q,ALT_R,ALT_S,ALT_T,ALT_U,ALT_V,ALT_W,ALT_X,
  14.     ALT_Y,ALT_Z,ALT_0,ALT_1,ALT_2,ALT_3,ALT_4,ALT_5,
  15.     ALT_6,ALT_7,ALT_8,ALT_9
  16. };
  17.  
  18. Keyboard::Keyboard()
  19. {
  20.     shift = GetShift();
  21.     keydown = False;
  22.     insert = True;
  23. }
  24.  
  25. /* ---- Test for keystroke ---- */
  26. Bool Keyboard::KeyHit()
  27. {
  28.     _AH = 1;
  29.     geninterrupt(KEYBRD);
  30.     return (Bool)((_FLAGS & ZEROFLAG) == 0);
  31. }
  32.  
  33. /* ---- Read a keystroke ---- */
  34. int Keyboard::GetKey()
  35. {
  36.     int c;
  37.     while (KeyHit() == False)
  38.         ;
  39.     if (((c = bioskey(0)) & 0xff) == 0)
  40.         c = (c >> 8) | 0x1080;
  41.     else
  42.         c &= 0xff;
  43.     return c & 0x10ff;
  44. }
  45.  
  46. /* ---------- read the keyboard shift status --------- */
  47. int Keyboard::GetShift()
  48. {
  49.     regs.h.ah = 2;
  50.     int86(KEYBRD, ®s, ®s);
  51.     return regs.h.al;
  52. }
  53.  
  54. /* ------ convert an Alt+ key to its letter equivalent ----- */
  55. int Keyboard::AltConvert(int c)
  56. {
  57.     int i, a = 0;
  58.     for (i = 0; i < 36; i++)
  59.         if (c == altconvert[i])
  60.             break;
  61.     if (i < 26)
  62.         a = 'a' + i;
  63.     else if (i < 36)
  64.         a = '0' + i - 26;
  65.     return a;
  66. }
  67.  
  68. Bool Keyboard::ShiftChanged()
  69. {
  70.     int sk = GetShift();
  71.     Bool rtn = (Bool) (sk != shift);
  72.     shift = sk;
  73.     return rtn;
  74. }
  75.  
  76. // ------ dispatch keyboard events
  77. void Keyboard::DispatchEvent()
  78. {
  79.     DFWindow *Kwnd = desktop.InFocus();
  80.     if (Kwnd == 0)
  81.         Kwnd = (DFWindow *) desktop.ApplWnd();
  82.  
  83.     if (ShiftChanged())
  84.            // ---- the shift status changed
  85.         Kwnd->ShiftChanged(GetShift());
  86.     if (KeyHit())    {
  87.         // --- a key was pressed
  88.         Kwnd->Keyboard(GetKey());
  89.         keydown = True;
  90.     }
  91.     if (keydown && (inp(0x60) & 0x80))    {
  92.         // ----- the keyboard was released
  93.         keydown = False;
  94.         Kwnd->KeyReleased();
  95.     }
  96. }
  97.  
  98.  
  99.  
  100.